import { Component, OnInit } from '@angular/core';
import { MessageService } from './../core/message.service';
import { ApiService } from '../core/api';
import { Router, ActivatedRoute } from '@angular/router';
import { DialogService, ToptipsService } from 'ngx-weui';
import { I18nService } from '../core/i18n/i18n.service';

@Component({
	templateUrl: './userCenter.html',
	styleUrls: ['./userCenter.less'],
})
export class UserCenterComponent implements OnInit {
	res: any = {};
	userDel: any = { userWalletForUserView: [] };
	active = 1;
	pageNo = 1;
	show = false;
	showAmount = false;
	orderList = [];
	payList = [];
	loading1 = true;
	loading2 = true;
	iData;
	constructor(private i: I18nService, private api: ApiService, private message: MessageService, private router: Router, private activeRouter: ActivatedRoute, private srv: DialogService, private toptips: ToptipsService) {
		this.message.sendMessage('my')
		this.i.get([
			'MSG.sfqxdd',
			'MSG.ddqxcg',
			'userCenterPage.gb',
			'userCenterPage.qxdd',
			'userCenterPage.txqxyy',
			'userCenterPage.fh',
			'userCenterPage.tj',
			'userCenterPage.type1',
			'userCenterPage.type2',
			'userCenterPage.type3',
			'userCenterPage.type4',
		]).subscribe(res => {
			this.iData = res;
		});
	}

	ngOnInit() {
		let map = {
			'充值成功': this.iData['userCenterPage.type1'],
			'充值中': this.iData['userCenterPage.type2'],
			'充值取消': this.iData['userCenterPage.type3'],
			'支付成功': this.iData['userCenterPage.type4']
		}
		this.userDel = this.activeRouter.snapshot.data.userDel;
		this.api.post('用户交易记录列表', { pageNo: 1 }).then(res => {
			const obj = {};
			const array = [];
			res['data'].list.forEach(element => {
				if (obj[element.createTime.split(" ")[0]]) {
					obj[element.createTime.split(" ")[0]].push(element);
				} else {
					obj[element.createTime.split(" ")[0]] = [];
					obj[element.createTime.split(" ")[0]].push(element);
				}
				if (element.type == 'DEPOSIT' && element.status == 'PAID') {
					element.title = map['充值成功'];
				} else if (element.type == 'DEPOSIT' && element.status == 'PENDING') {
					element.title = map['充值中'];
				} else if (element.type == 'DEPOSIT' && element.status == 'CANCELED') {
					element.title = map['充值取消'];
				} else if (element.type == 'TRAVEL_ORDER' && element.status == 'PAID') {
					element.title = map['支付成功'];
				}
			});
			for (const key in obj) {
				array.push({ date: key, list: obj[key] });
			}
			this.payList = array;

		});
		this.loading2 = false;
		this.orderListFn();
	}
	back() {
		history.back();
	}
	orderListFn() {
		this.loading1 = true;
		this.api.post('旅游订单列表', { pageNo: this.pageNo }).then(res => {
			res['data']['list'].forEach(element => {
				if (element.status === 'PENDING') {
					element.config = {
						leftTime: element.leftTime,
						prettyText: text => {
							console.log();
							const arr = text.split(":");
							return "<i>" + arr[0] + "</i> : <i>" + arr[1] + "</i> : <i>" + arr[2] + "</i>";
						}
					}
				}
			});
			this.orderList = this.orderList.concat(res['data']['list']);
			this.loading1 = false;
		});
	}
	dialog2Show = false;
	closethis() {
		this.dialog2Show = false;
	}
	onLHCZ() {
		this.dialog2Show = true;
	}
	openCopyAddress() {
		this.show = true;
	}
	closeCopyAddress() {
		this.show = false;
	}
	toggleAmount() {
		this.showAmount = !this.showAmount;
	}

	showThis(i) {
		if (this.active !== i) {
			this.active = i;
		}
	}
	goPay(item) {
		this.router.navigate(['/recharge'], {
			queryParams: {
				travelOrderId: item.id,
			},
		});
	}
	cancel(item) {
		this.srv
			.show({
				title: ' ',
				content: this.iData['MSG.sfqxdd'],
				cancel: this.iData['userCenterPage.gb'],
				confirm: this.iData['userCenterPage.qxdd'],
			})
			.subscribe((res: any) => {
				if (res.text == this.iData['userCenterPage.gb']) {
					return
				} else {
					this.srv
						.show({
							title: ' ',
							content: this.iData['userCenterPage.txqxyy'],
							cancel: this.iData['userCenterPage.fh'],
							confirm: this.iData['userCenterPage.tj'],
							type: 'prompt',
							input: 'text',
							inputPlaceholder: this.iData['userCenterPage.txqxyy'],
							inputRequired: true,
						})
						.subscribe((res: any) => {
							if (res.text == '返回') {
								return;
							}
							this.api.post('取消旅游订单', { travelOrderId: item.id, cancelMemo: res.result }).then(res => {
								this.toptips.success(this.iData['MSG.ddqxcg']);
								this.orderList = [];
								this.orderListFn();
							});
						});
				}
			});
	}
	goDetail(item) {
		if (item.status === 'PAID' || item.status === 'PREPARING' || item.status === 'COMPLETED' || item.status === 'RECEIVED') {
			this.router.navigate(['/orderDetail'], {
				queryParams: {
					travelOrderId: item.id,
				},
			});
		}
	}
	copyaddress() {
		const copyDom = document.getElementById('address');
		const range = document.createRange();
		range.selectNode(copyDom);
		window.getSelection().removeAllRanges();
		window.getSelection().addRange(range);
		document.execCommand('copy');
		window.getSelection().removeAllRanges();
		this.toptips.success('复制成功！');
	}
}
